﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using ESoft.DataAccess;
using ESoft.DataAccess.Models;

namespace ESoft.Service
{
    /// <summary>
    /// 基础服务类
    /// </summary>
    public class BaseService : IBaseService
    {
        private readonly IBaseReponsitory _baseReponsitory;

        /// <summary>
        /// 初始化
        /// </summary>
        public BaseService(IBaseReponsitory reponsitory)
        {
            _baseReponsitory = reponsitory;
        }

        /// <summary>
        /// 获取符合条件的第一个
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public Task<T> FirstOrDefaultAsync<T>(Expression<Func<T, bool>> predicate) where T : EntityBase
        {
            return _baseReponsitory.FirstOrDefaultAsync(predicate);
        }

        /// <summary>
        /// 添加记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public T Add<T>(T entity) where T : EntityBase
        {
            return _baseReponsitory.Add(entity);
        }

        /// <summary>
        /// 添加记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Task<T> AddAsync<T>(T entity) where T : EntityBase
        {
            return _baseReponsitory.AddAsync<T>(entity);
        }

        /// <summary>
        /// 汇总总行数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public int Count<T>(Expression<Func<T, bool>> predicate) where T : EntityBase
        {
            return _baseReponsitory.Count(predicate);
        }

        /// <summary>
        /// 异步汇总总行数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public Task<int> CountAsync<T>(Expression<Func<T, bool>> predicate) where T : EntityBase
        {
            return _baseReponsitory.CountAsync<T>(predicate);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entities"></param>
        /// <returns></returns>
        public bool DeleteBatch<T>(IEnumerable<T> entities) where T : EntityBase
        {
            return _baseReponsitory.DeleteBatch(entities);
        }

        /// <summary>
        /// 删除单个
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete<T>(T entity) where T : EntityBase
        {
            return _baseReponsitory.Delete(entity);
        }

        /// <summary>
        /// 更新单个
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Task<T> UpdateAsync<T>(T entity) where T : EntityBase
        {
            return _baseReponsitory.UpdateAsync<T>(entity);
        }

        /// <summary>
        /// 批量更新
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entitys"></param>
        /// <returns></returns>
        public bool UpdateBatch<T>(List<T> entitys) where T : EntityBase
        {
            return _baseReponsitory.UpdateBatch<T>(entitys);
        }

        /// <summary>
        /// 异步批量更新
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entitys"></param>
        /// <returns></returns>
        public async Task UpdateBatchAsync<T>(List<T> entitys) where T : EntityBase
        {
            await _baseReponsitory.UpdateBatchAsync<T>(entitys);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete<T>(object id) where T : EntityBase
        {
            return _baseReponsitory.Delete<T>(id);
        }

        /// <summary>
        /// 检查是否包含
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public bool Exist<T>(Expression<Func<T, bool>> predicate) where T : EntityBase
        {
            return _baseReponsitory.Exist<T>(predicate);
        }

        /// <summary>
        /// 查找单个
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public T Find<T>(object id) where T : EntityBase
        {
            return _baseReponsitory.Find<T>(id);
        }

        /// <summary>
        /// 异步查找单个
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public Task<T> FindAsync<T>(object id) where T : EntityBase
        {
            return _baseReponsitory.FindAsync<T>(id);
        }

        /// <summary>
        /// 获取符合条件的第一个
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public T First<T>(Expression<Func<T, bool>> predicate) where T : EntityBase
        {
            return _baseReponsitory.First(predicate);
        }

        /// <summary>
        /// 获取符合条件的总数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="selector"></param>
        /// <returns></returns>
        public Task<int> SumAsync<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, int>> selector) where T : EntityBase
        {
            return _baseReponsitory.SumAsync<T>(predicate, selector);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List<T> FindEntities<T>() where T : EntityBase
        {
            return _baseReponsitory.FindAll<T>().ToList();
        }

        /// <summary>
        /// 异步获取全部
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public Task<List<T>> QueryEntitiesAsync<T>() where T : EntityBase
        {
            return _baseReponsitory.FindAllAsync<T>();
        }

        /// <summary>
        /// 获取所有符合条件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public List<T> FindEntities<T>(Expression<Func<T, bool>> predicate) where T : EntityBase
        {
            return _baseReponsitory.FindEntities(predicate).ToList();
        }

        /// <summary>
        /// 异步获取符合条件的实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public Task<List<T>> FindEntitiesAsync<T>(Expression<Func<T, bool>> predicate) where T : EntityBase
        {
            return _baseReponsitory.FindEntitiesAsync<T>(predicate);
        }

        /// <summary>
        /// 获取符合条件的总数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="selector"></param>
        /// <returns></returns>
        public int Sum<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, int>> selector) where T : EntityBase
        {
            return _baseReponsitory.Sum(predicate, selector);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update<T>(T entity) where T : EntityBase
        {
            return _baseReponsitory.Update(entity);
        }

        /// <summary>
        /// 分页获取对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="page"></param>
        /// <param name="predicate"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public List<T> FindEntitiesByPage<T>(PageInput page, Expression<Func<T, bool>> predicate, SortVo sort)
            where T : EntityBase
        {
            return _baseReponsitory.FindEntitiesByPage(page, predicate, sort).ToList();
        }

        /// <summary>
        /// 异步分页获取对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="page"></param>
        /// <param name="predicate"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public async Task<List<T>> FindEntitiesByPageAsync<T>(PageInput page, Expression<Func<T, bool>> predicate, SortVo sort)
           where T : EntityBase
        {
            return await _baseReponsitory.FindEntitiesByPageAsync(page, predicate, sort);
        }
    }
}
